<?php

class ActionHelper_Abstract_Extend extends ActionHelper_Abstract_Basic {
	protected $_id;
	
	protected $_isPopulateFormData = true;
	protected $_objectnameInView = 'object';
	/**
	 * @var Module_Interface_Opration
	 * @access protected
	 */
	protected $_moduleOpration = null;
	protected $_oprationField = 'id';
	protected $_isCheckBeforeUpdate = false;
	protected $_isCheckBeforeDelete = false;
	protected $_oprationInsertFunction = 'insert';
	protected $_oprationDeleteFunction = 'delete';
	protected $_oprationUpdateFunction = 'updateByField';
	/**
	 * @var Module_Interface_Select
	 * @access protected
	 */
	protected $_moduleSelect = null;
	protected $_selectField = 'id';
	
	/**
	 * @var Module_Interface_Orderlist
	 * @access protected
	 */
	protected $_moduleOrderListOpration = null;
	/**
	 * @var Object_Abstract
	 * @access protected
	 */
	protected $_initObject = null;
	
	//action type
	const ACTION_LIST = 'list';
	const ACTION_NEW = 'new';
	const ACTION_DELETE = 'delete';
	const ACTION_EDIT = 'edit';
	const ACTION_UP = 'up';
	const ACTION_DOWN = 'down';
	
	protected $_actionType = null;
	
	//pagelist
	protected $_whereArray = null;
	
	protected $_isPagelist = true;
	
	protected $_pagenumber = 15;
	
	protected $_linklist = 15;
	
	/**
	 * @var Zend_Form_Element_Xhtml
	 * @access private
	 */
	protected $_form_at_result_page;
	protected $_session_name = 'search';
	
	protected $_updateToNoShow = false;
	protected $_updateToNoShowArray;
	
	
	const OPTIONS_POPULATE_FORMDATA = 'opulateFormData';
	const OPTIONS_OBJECTNAME_IN_VIEW = 'objectnameInView';
	const OPTIONS_INIT_OBJECT = 'initobject';
	const OPTIONS_INIT_ID = 'initobjectid';
	const OPTIONS_ACTION_TYPE = 'action_type';
	
	const OPTIONS_MODULE_SELECT = 'module_select';
	const OPTIONS_MODULE_SELECT_FIELD = 'module_select_field';
	
	const OPTIONS_MODULE_OPRATION = 'module_opration';
	const OPTIONS_MODULE_OPRATION_FIELD = 'module_opration_field';
	const OPTIONS_MODULE_OPRATION_UPDATE_CHECK = 'module_opration_update_check';
	const OPTIONS_MODULE_OPRATION_FUNCTION_UPDATE = 'module_opration_function_update';
	const OPTIONS_MODULE_OPRATION_DELETE_CHECK = 'module_opration_update_check';
	const OPTIONS_MODULE_OPRATION_FUNCTION_DELETE = 'module_opration_function_delete';
	const OPTIONS_MODULE_OPRATION_FUNCTION_INSERT = 'module_opration_function_insert';
	
	const OPTIONS_MODULE_ORDERLIST = 'module_orderlist';
	
	const OPTIONS_PAGELIST_WHERE_ARRAY = 'wherearray';
	const OPTIONS_PAGELIST_ENABLE = 'pagelist_enable';
	const OPTIONS_PAGELIST_PAGENUMBER = 'pagelist_pagenumber';
	const OPTIONS_PAGELIST_LINKLIST = 'pagelist_linklist';
	
	const OPTIONS_SEATCH_SESSION_NAME = 'search_session_name';
	const OPTIONS_FORM_AT_RESULT_PAGE= 'from_at_result_page';
	
	const OPTIONS_FALSEDELETE_ENABLE = 'false_delete';
	const OPTIONS_FALSEDELETE_ENABLE_INFO = 'false_delete_info';
	
	public function setOptions($options){
		parent::setOptions($options);
		
		if(isset($options[self::OPTIONS_OBJECTNAME_IN_VIEW])){
			$this->_objectnameInView = $options[self::OPTIONS_OBJECTNAME_IN_VIEW];
		}
		if(isset($options[self::OPTIONS_INIT_ID])){
			$this->view->id = $options[self::OPTIONS_INIT_ID];;
			$this->_id = $options[self::OPTIONS_INIT_ID];;
		}
		if(isset($options[self::OPTIONS_INIT_OBJECT])){
			$this->_initObject = $options[self::OPTIONS_INIT_OBJECT];
			if($this->_initObject instanceof Object_Abstract ){
				$this->view->id = $this->_initObject->id;
				$this->_id = $this->_initObject->id;
			}
		}
		if(isset($options[self::OPTIONS_POPULATE_FORMDATA])){
			$this->_isPopulateFormData = $options[self::OPTIONS_POPULATE_FORMDATA];		
		}
		if(isset($options[self::OPTIONS_MODULE_OPRATION])){
			$module = $options[self::OPTIONS_MODULE_OPRATION];
			if (! $module instanceof Module_Interface_Opration) {
				throw new Exception ( 'module must instance of Module_Interface_Opration' );
			}
			$this->_moduleOpration = $module;
		}
		if(isset($options[self::OPTIONS_MODULE_SELECT])){
			$module = $options[self::OPTIONS_MODULE_SELECT];
			if (! $module instanceof Module_Interface_Select) {
				throw new Exception ( 'module must instance of Module_Interface_Select' );
			}
			$this->_moduleSelect = $module;
		}
		if(isset($options[self::OPTIONS_MODULE_ORDERLIST])){
			$module = $options[self::OPTIONS_MODULE_ORDERLIST];
			if (! $module instanceof Module_Interface_Orderlist) {
				throw new Exception ( 'module must instance of Module_Interface_Orderlist' );
			}
			$this->_moduleOrderListOpration = $module;
		}
		if(isset($options[self::OPTIONS_MODULE_SELECT_FIELD])){
			$this->_selectField = $options[self::OPTIONS_MODULE_SELECT_FIELD];		
		}
		if(isset($options[self::OPTIONS_MODULE_OPRATION_FIELD])){
			$this->_oprationField = $options[self::OPTIONS_MODULE_OPRATION_FIELD];		
		}
		if(isset($options[self::OPTIONS_MODULE_OPRATION_DELETE_CHECK])){
			$this->_isCheckBeforeDelete = $options[self::OPTIONS_MODULE_OPRATION_DELETE_CHECK];		
		}
		if(isset($options[self::OPTIONS_MODULE_OPRATION_UPDATE_CHECK])){
			$this->_isCheckBeforeUpdate = $options[self::OPTIONS_MODULE_OPRATION_UPDATE_CHECK];		
		}
		if(isset($options[self::OPTIONS_MODULE_OPRATION_FUNCTION_DELETE])){
			$this->_oprationDeleteFunction = $options[self::OPTIONS_MODULE_OPRATION_FUNCTION_DELETE];		
		}
		if(isset($options[self::OPTIONS_MODULE_OPRATION_FUNCTION_UPDATE])){
			$this->_oprationUpdateFunction = $options[self::OPTIONS_MODULE_OPRATION_FUNCTION_UPDATE];		
		}
		if(isset($options[self::OPTIONS_MODULE_OPRATION_FUNCTION_INSERT])){
			$this->_oprationInsertFunction = $options[self::OPTIONS_MODULE_OPRATION_FUNCTION_INSERT];		
		}
		if(isset($options[self::OPTIONS_ACTION_TYPE])){
			$this->_actionType = $options[self::OPTIONS_ACTION_TYPE];		
		}
		if(isset($options[self::OPTIONS_PAGELIST_WHERE_ARRAY])){
			$this->_whereArray = $options[self::OPTIONS_PAGELIST_WHERE_ARRAY];		
		}
		if(isset($options[self::OPTIONS_PAGELIST_ENABLE])){
			$this->_isPagelist = $options[self::OPTIONS_PAGELIST_ENABLE];		
		}
		if(isset($options[self::OPTIONS_PAGELIST_PAGENUMBER])){
			$this->_pagenumber = $options[self::OPTIONS_PAGELIST_PAGENUMBER];		
		}
		if(isset($options[self::OPTIONS_PAGELIST_LINKLIST])){
			$this->_linklist = $options[self::OPTIONS_PAGELIST_LINKLIST];		
		}
		if(isset($options[self::OPTIONS_FORM_AT_RESULT_PAGE])){
			$this->_form_at_result_page = $options[self::OPTIONS_FORM_AT_RESULT_PAGE];		
		}
		if(isset($options[self::OPTIONS_SEATCH_SESSION_NAME])){
			$this->_session_name = $options[self::OPTIONS_SEATCH_SESSION_NAME];		
		}
		if(isset($options[self::OPTIONS_FALSEDELETE_ENABLE])){
			$this->_updateToNoShow = $options[self::OPTIONS_FALSEDELETE_ENABLE];		
		}
		if(isset($options[self::OPTIONS_FALSEDELETE_ENABLE_INFO])){
			$this->_updateToNoShowArray = $options[self::OPTIONS_FALSEDELETE_ENABLE_INFO];		
		}
	}	
		
	public function isCheckBeforeUpdate() {
		return $this->_isCheckBeforeUpdate;
	}
	
	public function isCheckBeforeDelete() {
		return $this->_isCheckBeforeDelete;
	}
		
	public function isPopulateFormData($bool) {
		if (is_bool ( $bool )) {
			$this->_isPopulateFormData = $bool;
		}
	}
	
	
	protected function isDeleteProcess() {
		if ($this->_actionType == self::ACTION_DELETE) {
			if (empty ( $this->_id )) {
				throw new Exception ( 'id is null' );
			}
		}
		return ($this->_actionType == self::ACTION_DELETE);
	}
	protected function isRegisterProcess() {
		if (empty ( $this->_actionType )) {
			return empty ( $this->_id );
		}
		return ($this->_actionType == self::ACTION_NEW);
	}
	
	protected function isEditProcess() {
		if (empty ( $this->_actionType )) {
			return ! empty ( $this->_id );
		}
		if ($this->_actionType == self::ACTION_EDIT) {
			if (empty ( $this->_id )) {
				throw new Exception ( 'id is null' );
			}
			return true;
		}
		return false;
	}
	
	protected function isOrderUpProcess() {
		if ($this->_actionType == self::ACTION_UP) {
			if (empty ( $this->_id )) {
				throw new Exception ( 'id is null' );
			}
			return true;
		}
		return false;
	}
	
	protected function isOrderDownProcess() {
		if ($this->_actionType == self::ACTION_DOWN) {
			if (empty ( $this->_id )) {
				throw new Exception ( 'id is null' );
			}
			return true;
		}
		return false;
	}
}